﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace Jon.Adventureworks.Core.ExtensionMethods
{
    /// <summary>
    /// Class in includes extension methods for <see cref="System.Collections.Generic"/> types.
    /// </summary>
    public static class SystemCollectionsGenericExtensions
    {
        /// <summary>
        /// Takes a base System.Collections.Generic List<T> and returns a Xml string representation.
        /// <see cref="System.String"/> using a <see cref="System.Xml.Serialization.XmlSerializer"/>
        /// </summary>
        /// <param name="list">List to serialize.</param>
        /// <returns>string in xml</returns>
        public static string ToXmlString<T>(this List<T> list)
        {
            //Type[] extraTypes = new Type[7] { typeof(Product), typeof(SleepProfile), typeof(Topic), typeof(Category), typeof(ContentGroup), typeof(LinkNode), typeof(Phrase) };
            Type[] extraTypes = null;
            XmlRootAttribute root = new XmlRootAttribute(typeof(T).Name + "List");
            XmlSerializer serializer = new XmlSerializer(typeof(List<T>), null, extraTypes, root, "");
            TextWriter tw = new StringWriter();

            serializer.Serialize(tw, list);
            tw.Close();
            return tw.ToString();
        }
    }
}
